// 这里存放的是home页的路由
import store from '../../store'
import { Message } from 'element-ui'
import userRoute from './user'
function isLogin (to, form, next) {
  // 用户已经登入了
  if (store.getters.getIsLogin) {
    Message({
      message: '您已经登入了无需再次注册',
      type: 'info'
    })
    return next(form.path)
  }
  return next()
}

/**
 * 判断用户是否登入中间件
 * @param to
 * @param form
 * @param next
 */
function auth (to, form, next) {
  if (store.getters.getIsLogin) {
    return next()
  } else {
    Message({
      message: '请登入后再试',
      type: 'waring'
    })
    return next({ name: 'login' })
  }
}

export default [
  {
    // 主页
    path: '/',
    name: 'home',
    component: () => import('../../views/Home/index'),
    children: [
      {
        // 首页
        path: '',
        name: 'home',
        component: () => import('../../views/Home/Home')
      },
      {
        // 首页
        path: 'home',
        name: 'home',
        component: () => import('../../views/Home/Home')
      },
      {
        // 登入
        path: '/login',
        name: 'login',
        component: () => import('../../views/Home/Authorization/Login'),
        beforeEnter: isLogin
      },
      {
        // 注册
        path: '/register',
        name: 'register',
        component: () => import('../../views/Home/Authorization/Registered'),
        beforeEnter (to, form, next) {
          // 用户已经登入了
          if (store.getters.getIsLogin) {
            Message({
              message: '您已经登入了无需再次注册',
              type: 'info'
            })
            return next(form.path)
          }
          return next()
        }
      },
      {
        // 商品列表页
        path: '/product',
        name: 'product',
        component: () => import('../../views/Home/product/Product')
      },
      {
        // 商品列表页
        path: '/category/:id?',
        name: 'category',
        component: () => import('../../views/Home/product/Product')
      },
      {
        // 商品详情
        path: '/product/:id',
        name: 'product.show',
        component: () => import('../../views/Home/product/Show')
      },
      {
        // 用户中心首页
        path: '/user',
        name: 'user.index',
        component: () => import('../../views/Home/user/Index'),
        beforeEnter: auth,
        children: userRoute
      }
    ]
  }

]
